<?php
/**
 * Created by PhpStorm.
 * User: Ellon_Wong
 * Date: 14-4-25
 * Time: 下午10:32
 */
//include_once dirname(__FILE__) . '/../../lib/Log4PHP/Log4PHP.php';
include_once dirname(__FILE__) . '/../common/dbConn.php';
//include_once dirname(__FILE__) . '/../common/constant.php';

global $msgArray;
//global $log;

//$log = Log4PHP::getLogger("reglog");
function register_func($arg_body){
    try{
        global $log;
        global $msgArray;
//        $log->info("===================register_func begin=====================");
//        $log->info("传入参数：");
//        $log->info($arg_body);
        if(!array_key_exists("userAccount",$arg_body)){
            return MsgArray(false,"传入用户名为空",[],"Warn");
        }

        if(!array_key_exists("userPassword",$arg_body)){
            return MsgArray(false,"传入密码为空",[],"Warn");
        }

        $rsl = MsgArray(false,"操作失败!",[],"Warn");

        $q_pdo=connectDatabase();
        if(null==$q_pdo){
            return MsgArray(false,"数据库连接异常",[],"Warn");
        }
        $q_pdo->beginTransaction();

        $req_ckSql = "SELECT ID FROM T_USER WHERE U_ACCOUNT = '".$arg_body["userAccount"]."'";
        $rs = $q_pdo->prepare($req_ckSql);
        $rs->execute();
        $rsl = $rs->fetch();
        if(false!=$rsl){
            return MsgArray(false,"用户已存在！","","Error");
        }

        $reg_sql = "INSERT INTO T_USER (U_ACCOUNT,U_PASSWORD,U_STATUS) VALUES (?,?,?)";

//        $log->info($reg_sql);
        $stmt = $q_pdo->prepare($reg_sql);
        $num=$stmt->execute(array($arg_body["userAccount"],$arg_body["userPassword"],"active"));
      if($num<=0){
          return MsgArray(false,"插入注册数据失败！","","Error");
      }
        $userId = $q_pdo->lastInsertId();
        $q_pdo->commit();
        return MsgArray(true,"注册成功",$userId,"Success");

    }catch (Exception $e){
        $log->error("抛出异常:".$e->getMessage());
        return MsgArray(100001,"系统异常",[],"Error");
    }
}

